#include "../mqserver/mq_route.hpp"
#include <gtest/gtest.h>
#include <iostream>

class RouteTest : public testing::Environment 
{
public:
    virtual void SetUp() override { std::cout << "init finish!\n"; }
    virtual void TearDown() override { /* exmp->clear(); */ std::cout << "clear finish!\n"; }
};

TEST(route_test, legal_routing_key) 
{
    ASSERT_EQ(mqserver::Router::isLegalRoutingKey("news.music.pop"), true);  
    ASSERT_EQ(mqserver::Router::isLegalRoutingKey("news..music..pop"), true);    // 字符串分割做了容错处理
    ASSERT_EQ(mqserver::Router::isLegalRoutingKey("news.,music.pop"), false);    
    ASSERT_EQ(mqserver::Router::isLegalRoutingKey("news..music_1.pop"), true);
}

TEST(route_test, legal_binding_key) 
{
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.pop"), true);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.*"), true);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.#"), true);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.#.*"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.*.#"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.#.#"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music.*.*"), true);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.,music_1.pop"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news.music_1.*#"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news*.music_1.pop"), false);
    ASSERT_EQ(mqserver::Router::isLegalBingdingKey("news#.music_1.pop"), false);
}
TEST(route_test, legal_route) 
{
    std::vector<std::string> bkeys = {
        "aaa",
        "aaa.bbb",
        "aaa.bbb", 
        "aaa.bbb",
        "aaa.#.bbb",
        "aaa.bbb.#",
        "#.bbb.ccc",
        "aaa.bbb.ccc",
        "aaa.*",
        "aaa.*.bbb",
        "*.aaa.bbb", 
        "#",   
        "aaa.#", 
        "aaa.#",  
        "aaa.#.ccc",
        "aaa.#.ccc",
        "aaa.#.ccc",
        "#.ccc",
        "#.ccc",
        "aaa.#.ccc.ccc",
        "aaa.#.bbb.*.bbb",
        "aaa.*.*.*.bbb.#.aaa.*"
    };

    std::vector<std::string> rkeys = {
        "aaa",
        "aaa.bbb",    
        "aaa.bbb.ccc",        
        "aaa.ccc",        
        "aaa.bbb.ccc",        
        "aaa.ccc.bbb",        
        "aaa.bbb.ccc.ddd",    
        "aaa.bbb.ccc",       
        "aaa.bbb",         
        "aaa.bbb.ccc",      
        "aaa.bbb",         
        "aaa.bbb.ccc",       
        "aaa.bbb",        
        "aaa.bbb.ccc",     
        "aaa.ccc",        
        "aaa.bbb.ccc",       
        "aaa.aaa.bbb.ccc",  
        "ccc",         
        "aaa.bbb.ccc",    
        "aaa.bbb.ccc.ccc.ccc",
        "aaa.ddd.ccc.bbb.eee.bbb",
        "aaa.ccc.eee.ddd.bbb.aaa"
    };

    std::vector<bool> result = {
       true,
        true,
        false,
        false,
        false,
        false,
        false,
        true,
        true,
        false,
        false,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        false
    };
    for (int i = 0; i < bkeys.size(); i++) {
        ASSERT_EQ(
            mqserver::Router::route(proto::message::ExchangeType::TOPIC, rkeys[i], bkeys[i]), result[i]
        );
    }
}


int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new RouteTest());
    (void)RUN_ALL_TESTS();
    return 0;
}